<?php
    class Default_Model_Sales extends Zend_Db_Table_Abstract
    {
        protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
        
        public function listItem()
        {
            $data = array(
                'id',
                'type',
                'type_id',
                'sales',
            );
            $sql = $this->_db->select()
                            ->from('sales', $data)
                            ->where('status = 1')
                            ->order('id DESC');
            return $sql;
        }
        
        public function getSalesByType($type, $type_id, $locale)
        {
            $sales = array(
                'sales',
                'start_date',
                'finish_date',
            );
            $sales_language = array(
                'description',
            );
            $sql = $this->_db->select()
                            ->from('sales AS s', $sales)
                            ->join('sales_language AS sl', 'sl.sales_id = s.id', $sales_language)
                            ->join('language AS l', 'l.id = sl.language_id', null)
                            ->where('s.type = ?', $type)
                            ->where('s.type_id = ?', $type_id)
                            ->where('s.status = 1')
                            ->where('l.locale = ?', $locale)
                            ->where('DATEDIFF(finish_date, NOW()) > 0');
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getHotelByType($type, $type_id, $locale)
        {
            $data = array(
                'id',
                'star',
                'image',
            );
            $data_language = array(
                'name',
                'address',
            );
            switch($type) {
                case    1   :   {
                    $name = 'hotel';
                    array_push($data, 'tag_id');
                    break;
                }
                case    2   :   {
                    $name = 'boating';
                    break;
                }
                case    3   :   {
                    $name = 'resort';
                    array_push($data, 'tag_id');
                    break;
                }
            }
            $sql = $this->_db->select()
                            ->from($name.' AS h')
                            ->join($name.'_language AS hl', 'hl.'.$name.'_id = h.id')
                            ->join('language AS l', 'hl.language_id = l.id', null)
                            ->where('h.status = 1')
                            ->where('h.id = ?', $type_id)
                            ->where('l.locale = ?', $locale);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getTypeById($type)
        {
            switch($type) {
                case    1   :   $result = 'hotel'; break;
                case    2   :   $result = 'boating'; break;
                case    3   :   $result = 'resort'; break;
            }
            return $result;
        }
    }
